import { Component, OnInit } from '@angular/core';
import {Movie} from '../movie'
import {MoviesService} from '../movies.service';
import { NzMessageService } from 'ng-zorro-antd/message';
import {FormControl} from "@angular/forms";
@Component({
  selector: 'app-movies',
  templateUrl: './movies.component.html',
  styleUrls: ['./movies.component.css']
})
export class MoviesComponent implements OnInit {
  keyword='';
  username='';
  movies= [];
  onlinePage=0;
  showOnLineMovies=[];
  size=6;
  OutLineMovies=[];
  showOutLineMovies=[];
  outLinePage:0;
  searchList:Movie[];
  hotMovies:Movie[];
  showHotMovies=[];
  hotPage:0;
  interest=[];
  myControl = new FormControl();
  //获取实时推荐
  getOnlineMovies(){
    this.moviesService.getOnLineMovies(this.username).subscribe(res=>{
      console.log("实时推荐电影：",res);
      if(res.code==0){
        this.movies=res.data;

        this.onlinePage=0;
        this.getOnLineShow();
      }else{
        this.message.create('error',res.msg);
        this.movies=null;
      }
    })
  }
  //根据页码截取实时推荐的显示电影
  getOnLineShow(){
    this.showOnLineMovies=this.movies.slice(this.onlinePage*this.size,(this.onlinePage+1)*this.size);
  }
  //实时推荐前翻
  onLinePre(){
    if(this.onlinePage==0){
      this.message.create('warning','翻到头了哦！');
    }else{
      this.onlinePage--;
      this.getOnLineShow();
    }
  }
  //实时推荐后翻
  onLineDre(){
    let end=this.movies.length/this.size;
    if(this.onlinePage>=end-1){
      this.message.create('warning','翻到底了哦！');
    }else{
      this.onlinePage++;
      this.getOnLineShow();
    }
  }
  //根据页码截取离线推荐的显示电影
  getOutLineShow(){
    this.showOutLineMovies=this.OutLineMovies.slice(this.outLinePage*this.size,(this.outLinePage+1)*this.size)
    //console.log("截取离线电影：",this.showOutLineMovies);
  }
  //离线推荐前翻页
  outLinePre(){
    if (this.outLinePage===0){
      this.message.create('warning','翻到头了哦！');
    }else{
      this.outLinePage --;
      this.getOutLineShow();
    }
  }
  //离线推荐后翻页
  outLineDre(){
    let end= this.OutLineMovies.length/this.size;
    if (this.outLinePage>=end-1){
      this.message.create('warning','翻到底了哦！');
    }else{
      this.outLinePage ++;
      this.getOutLineShow();
    }
  }
  //根据页码截取热门推荐的显示电影
  getHotShow(){
    this.showHotMovies=this.hotMovies.slice(this.hotPage*this.size,(this.hotPage+1)*this.size)
  }
  //热门推荐前翻
  hotPre(){
    if (this.hotPage===0){
      this.message.create('warning','翻到头了哦！');
    }else{
      this.hotPage --;
      this.getHotShow();
    }
  }
  //热门推荐后翻
  hotDre(){
    let end= this.hotMovies.length/this.size;
    if (this.hotPage>=end-1){
      this.message.create('warning','翻到底了哦！');
    }else{
      this.hotPage ++;
      this.getHotShow();
    }
  }
  //获取离线推荐
 getOutLineMovies(){
   //获取用户信息（取其中的用户感兴趣电影类型）
   this.moviesService.getUserInfo(this.username).subscribe(res=>{
     console.log("用户的信息：",res)
     if(res.code==0){
       this.interest = res.data.interest;
       console.log("兴趣",this.interest);
       for (let i of this.interest){
         this.getMoviesByInterest(i);
       }

     }else{
       this.message.create('error',res.msg);
     }
   })
  }
  //根据感兴趣类型获取推荐电影
  getMoviesByInterest(movieType:string){
    this.moviesService.getSimilarMoviesByTypes(movieType).subscribe(res=>{
      console.log("由:"+movieType+" 推荐的电影：",res);
      if(res.code==0){
        //this.movies.concat(res.data)
        for (let m of res.data){
          this.OutLineMovies.push(m);
        }
        console.log('离线电影推荐：',this.OutLineMovies);
        this.outLinePage=0;
        this.getOutLineShow();
      }else{
        this.OutLineMovies=null;
        this.message.create('error',res.msg);
      }
    })
  }
  //获取当月热门推荐
  getHotMovies(){
    this.moviesService.getHotMovies().subscribe(res=>{
      console.log("当月最热：",res);
      if(res.code==0){
        this.hotMovies=res.data;
        this.hotPage=0;
        this.getHotShow();
      }else{
        this.hotMovies=null;
        this.message.create('error',res.msg);
      }
    })
  }
  //根据输入框内容搜索电影
  getSearchList(){
    console.log("关键字：",this.keyword);
    this.moviesService.getSearchMovies(this.keyword).subscribe(res=>{
      if(res.code==0){
        this.searchList=res.data;
      }else{
        this.message.create('error',res.msg);
      }
    })
  }
  constructor(private moviesService:MoviesService,private message: NzMessageService) { }
  ngOnInit(): void {
   // this.getMovies()
    this.username=window.localStorage['username'];
    this.getOutLineMovies()
    this.getHotMovies();
    this.getOnlineMovies();

  }

}
